<%@ page import="com.niit.service.MachineService" %>
<%@ page import="java.util.List" %>
<%@ page import="com.niit.pojo.Machine" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1 , user-scalable=no">
    <title>机器管理 - pha-Admin</title>
    <link rel="stylesheet" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/bootstrap-maizi.css"/>
    <link rel="stylesheet" href="css/content-style.css"/>
    <link rel="stylesheet" href="css/mricode.pagination.css"/>
    <link rel="stylesheet" href="css/jquery.fancybox.css"/>
    <link rel="stylesheet" href="css/sweetalert.css"/>
</head>
<body>
<!--导航-->
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a href="index.jsp" class="navbar-brand"> pha-Admin</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li><a href="index.jsp"><span class="glyphicon glyphicon-home"></span>&nbsp;&nbsp;后台首页</a></li>
                <li><a href="/user"><span class="glyphicon glyphicon-user"></span>&nbsp;&nbsp;用户管理</a></li>
                <li class="active"><a href="machine.jsp"><span class="glyphicon glyphicon-expand"></span>&nbsp;&nbsp;机器管理</a></li>
                <li><a href="service.jsp"><span class="glyphicon glyphicon-tasks"></span>&nbsp;&nbsp;服务管理</a></li>
                <li><a href="log.jsp"><span class="glyphicon glyphicon-list-alt"></span>&nbsp;&nbsp;日志管理</a></li>
                <li><a href="wiki.jsp"><span class="glyphicon glyphicon-book"></span>&nbsp;&nbsp;使用文档</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li class="dropdown">
                    <a id="dLabel" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <%
                            String displayName = (String) session.getAttribute("displayName");
                            out.print(displayName != null ? displayName : "admin");
                        %>
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu" aria-labelledby="dLabel">
                        <li><a href="index.jsp"><span class="glyphicon glyphicon-cog"></span>&nbsp;&nbsp;个人设置</a></li>
                    </ul>
                </li>
                <li><a href="#bbs"><span class="glyphicon glyphicon-off"></span>&nbsp;&nbsp;退出</a></li>
            </ul>
        </div>
    </div>
</nav><!--导航-->
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="panel-body" style="padding: 15px 0;">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#list" aria-controls="machine" role="tab" data-toggle="tab">机器列表</a></li>
                    <li><a href="#form" aria-controls="machine" role="tab" data-toggle="tab">添加机器</a></li>
                </ul>
            </div>
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="list">
                    <div class="panel-body">
                        <div class="row">
                            <form action="machine" method="get">
                                <div class="col-md-3 col-lg-offset-9">
                                    <div class="input-group">
                                        <input class="form-control" type="text"
                                               value="${searchKeyword != null ? searchKeyword : ''}"
                                               placeholder="输入域名或IP搜索" name="keyword">
                                        <span class="input-group-btn">
                                            <button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-search"></i> 搜索</button>
                                        </span>
                                    </div>
                                </div>
                            </form>
                        </div>

                        <!-- 搜索结果提示 -->
                        <c:if test="${searchKeyword != null and not empty searchKeyword}">
                            <div class="alert alert-info">
                                搜索结果: " ${searchKeyword} "
                                <c:if test="${empty machines}">
                                    <span class="text-danger"> - 未找到匹配的机器</span>
                                </c:if>
                            </div>
                        </c:if>
                    </div>
                    <div class="panel panel-default">
                        <div class="table-responsive">
                            <table class="table table-bordered" id="machineTable">
                                <thead>
                                <tr>
                                    <th class="w8p">ID</th>
                                    <th class="w18p">域名</th>
                                    <th class="w18p">IP</th>
                                    <th class="w13p">Sphinx路径</th>
                                    <th class="w10p">备注</th>
                                    <th class="w15p">创建时间</th>
                                    <th class="w15p">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <c:choose>
                                    <c:when test="${not empty machines}">
                                        <c:forEach items="${machines}" var="machine">
                                            <tr>
                                                <td class="center">${machine.id}</td>
                                                <td>${machine.domain}</td>
                                                <td>${machine.ip}</td>
                                                <td>${machine.sphinxPath}</td>
                                                <td class="center">${machine.remark}</td>
                                                <td class="center">${machine.createTime}</td>
                                                <td class="center">

                                                    <a href="javascript:;" onclick="deleteMachine(${machine.id})"><i class="glyphicon glyphicon-remove"></i>删除</a>
                                                </td>
                                            </tr>
                                        </c:forEach>
                                    </c:when>
                                    <c:otherwise>
                                        <tr>
                                            <td colspan="7" class="text-center">没有找到机器数据</td>
                                        </tr>
                                    </c:otherwise>
                                </c:choose>
                                </tbody>
                            </table>
                        </div>
                        <div class="panel-footer">
                            <div class="row">
                                <div class="col-md-8 m-pagination" id="paginator">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div role="tabpanel" class="tab-pane" id="form">
                    <div class="panel-body">
                        <c:if test="${not empty error}">
                            <div class="alert alert-danger" role="alert">
                                    ${error}
                            </div>
                        </c:if>
                        <form class="form-horizontal" action="machine" method="post">
                            <input type="hidden" name="action" value="add">
                            <div class="form-group">
                                <label class="col-sm-1 control-label"><span class="text-danger">*</span>域名</label>
                                <div class="col-sm-4">
                                    <input type="text" name="domain" class="form-control" placeholder="机器域名" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label"><span class="text-danger">*</span>IP</label>
                                <div class="col-sm-4">
                                    <input type="text" name="ip" class="form-control" placeholder="机器IP" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label"><span class="text-danger"></span>Sphinx<br>安装目录</label>
                                <div class="col-sm-4">
                                    <input type="text" name="sphinxPath" class="form-control" placeholder="Sphinx安装目录">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label"><span class="text-danger"></span>备注信息</label>
                                <div class="col-sm-4">
                                    <input type="text" name="remark" class="form-control" placeholder="备注信息">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-offset-1 col-sm-10">
                                    <button type="submit" class="btn btn-success">保存</button>
                                    <a href="machine" class="btn btn-default">取消</a>
                                </div>
                            </div>
                        </form>
                        <hr>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<footer>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <p class="text-muted center">
                    Copyright&nbsp;©&nbsp;2017-2025&nbsp;&nbsp;phachon@163.com&nbsp;&nbsp;
                </p>
            </div>
        </div>
    </div>
</footer>
<!--footer-->
<script src="js/plugins/jquery/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/plugins/pagination/mricode.pagination.js"></script>
<script src="js/plugins/fancybox/jquery.fancybox.js"></script>
<script src="js/plugins/sweetalert/sweetalert.min.js"></script>
<script src="js/common/common.js"></script>
<!-- 替换旧版JS -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script type="text/javascript">
    // 初始化分页
    var pageData = [];
    pageData.push({'pageSize': '10', 'total': '100', 'pageIndex': '1'});
    Common.paginator("#paginator", pageData);

    function deleteMachine(id) {
        Swal.fire({  // 注意Swal.fire替换旧版swal
            title: "确认删除?",
            text: "此操作将永久删除该机器信息，无法恢复！",
            icon: "warning",
            showCancelButton: true,
            confirmButtonText: "删除",
            cancelButtonText: "取消",
            dangerMode: true
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: "machine?action=delete&id=" + id,
                    type: "GET",
                    success: function(response) {
                        if (response.includes("删除成功")) {
                            Swal.fire("已删除！", "该机器信息已被成功删除。", "success")
                                .then(() => location.reload());
                        } else {
                            Swal.fire("删除失败", response, "error");
                        }
                    },
                    error: function(xhr, status, error) {
                        Swal.fire("删除错误", "服务器错误：" + error, "error");
                    }
                });
            }
        });
    }
</script>
</body>
</html>